﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Configuration;
using KPIS.DBM;
using KPIS.GERP.GHRMS.IDAL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.DAL
{
    public sealed class JobSiteTypeDAL : IJobSiteTypeDAL
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public JobSiteTypeDAL()
        {

        }
        public IList<JobSiteTypeInfo> ListJobSiteTypes(Nullable<int> Seq)
        {
            string strSQL;

            List<JobSiteTypeInfo> infoList = new List<JobSiteTypeInfo>();

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@DeleteRecord", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@Seq", Seq);

                strSQL = "SELECT jt.JOB_SITE_TYPE_SEQ, jt.JOB_SITE_TYPE_NAME, jt.CURR_JOB_SITE_SEQ, jt.REMARKS"
                    + ", j.job_site_name, j.upper_job_site_seq, uj.job_site_name as upper_job_site_name"
                    + " FROM HRM_MP_JOB_SITE_TYPES jt"
                    + " left join hrm_mp_job_sites j on jt.curr_job_site_seq = j.job_site_seq"
                    + " left join hrm_mp_job_sites uj on j.upper_job_site_seq = uj.job_site_seq"
                    + " WHERE jt.RECORD_STATUS not like @DeleteRecord"
                    + (Seq.HasValue ? " AND jt.JOB_SITE_TYPE_SEQ = @Seq" : "");

                DataTable dtTest = dbManager.ExecuteDataTable(CommandType.Text, strSQL);
                IDataReader rdr = dbManager.ExecuteReader(CommandType.Text, strSQL);

                while (rdr.Read())
                {
                    JobSiteTypeInfo Info = new JobSiteTypeInfo()
                    {
                        Sequence = rdr.GetInt32(0),
                        Name = rdr.IsDBNull(1) ? "" : rdr.GetString(1),
                        CurrentJobSites = rdr.GetInt32(2),
                        CurrentUpperJobSiteSeq = rdr.IsDBNull(5) ? (int?)null : rdr.GetInt32(5),
                        CurrentJobSiteName = rdr.IsDBNull(4) ? string.Empty : rdr.GetString(4),
                        CurrentUpperJobSiteName = rdr.IsDBNull(6) ? string.Empty : rdr.GetString(6)
                    };

                    infoList.Add(Info);
                }

            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }
            return infoList;
        }
       
    }
}
